/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2022并保留所有权利。
 * Copyright 2022, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.suanfa.test.teshuawei;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class TmpMain {
    public static String getLongDict(String str, String[] dict) {
        String dist = "";
        int flag = 0;
        for (int i = 0; i < dict.length; i++) {
            String tmp = dict[i];
            //tmp --a  able
            int length = tmp.length();

            //a
            char[] chars = str.toCharArray();
            if (length == 1) {
                AtomicInteger nu = new AtomicInteger();
                for (char c : chars) {
                    if (tmp.contains(c + "")) {
                        nu.getAndIncrement();
                    } else {
                        break;
                    }
                }
                if (flag < nu.intValue()) {
                    flag = nu.intValue();
                    dist = tmp;
                }
//                sMap.put(tmp,nu.intValue());
            } else {
                // tmp able
//            str    chars
                char[] t = tmp.toCharArray();
                int len1 = tmp.length();
                boolean f1 = false;
                for (char s : t) {
                    if (!str.contains(s + "")) {
                        f1 = true;
                        break;
                    }
                }
                if(f1){
                    continue;
                }

                if (flag < len1) {
                    flag = len1;
                    dist = tmp;
                }
            }

        }

        return dist;
    }

    public static void main(String[] args) {
        String str = "abpcplea";
        String[] dict = new String[]{"ale", "apple", "monkey", "plea"};
        System.out.println(getLongDict(str, dict));
    }
}
